package com.honeycomb.map;

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.core.io.ClassPathResource;

public class MapConfig {
	
	protected Logger logger = LogManager.getLogger(this.getClass());

	private String configLocation;

	private Properties config;
	
	public MapConfig() {
		config = new Properties();
	}
	
	public void initialize() {
		InputStream in = null;
		try {
			in = new ClassPathResource(configLocation).getInputStream();
			config.load(in);
		}
		catch(Exception ex) 
		{
			logger.error("Fail to load Configuration file " + configLocation, ex);
		}
		finally {
			if (in != null) 
			{
				try {
					in.close();						
				}
				catch(Exception ex)
				{
					logger.error("Fail to close InputStream from " + configLocation, ex);
				}
			}
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("MapConfig is loaded successfully. Property contains: " + config);
		}
	}
	
	public String getValue(String name) {
		String value = config.getProperty(name);
		if (value != null) {
			return value;
		}
		else {
			throw new IllegalArgumentException(name + " is not defined in Map Configuration.");
		} 
	}

	public String getConfigLocation() {
		return configLocation;
	}

	public void setConfigLocation(String configLocation) {
		this.configLocation = configLocation;
	}
	
}
